package cn.xuchunh.exam;

import java.util.Scanner;

/**
 * Created by XuChunH on 2016/9/9.
 */
public class WalkOut {

    public static void main(String[] args) {
        Scanner scanner  = new Scanner(System.in);
        while (scanner.hasNext()){
            int length = scanner.nextInt();
            int[][] matrix = new int[length][length];
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < length; j++) {
                    matrix[i][j] = scanner.nextInt();
                }
            }
            if(matrix[0][0] == 1 || matrix[length - 1][length - 1] == 1) {
                System.out.println(-1);
            } else {
                System.out.println(walkOut(matrix, 0, 0, length));
            }
        }
    }

    private static int walkOut(int[][] matrix, int x, int y, int des){
        if((x + 2 == des && y + 1 == des) || (x + 1 == des && y + 2 == des)) {
            return 1;
        }
        int num = -1;
        int can;
        if(x + 1 < des && matrix[x + 1][y] == 0){
            can = walkOut(matrix, x + 1, y, des);
            if(can != -1){
                num = can + 1;
            }
        }
        if(y + 1 < des && matrix[x][y + 1] == 0 && num == -1) {
            can = walkOut(matrix, x, y + 1, des);
            if(can != -1){
                num = can + 1;
            }
        }
        if(y - 1 >= 0 && matrix[x][y - 1] == 0 && num == -1){
            can = walkOut(matrix, x, y - 1, des);
            if(can != -1){
                num = can + 1;
            }
        }


        return num;
    }

}
